class Solution
{
public:
    long long maximumTripletValue(vector<int> &nums)
    {
        int n = nums.size();
        vector<int> leftMax(n);
        vector<int> rightMax(n);
        leftMax[0] = nums[0];
        for (int i = 1; i < n; i++)
        {
            leftMax[i] = max(leftMax[i - 1], nums[i]);
        }
        rightMax[n - 1] = nums[n - 1];
        for (int i = n - 2; i >= 0; i--)
        {
            rightMax[i] = max(rightMax[i + 1], nums[i]);
        }

        long long result = 0;
        for (int i = 1; i < n - 1; i++)
        {
            result = max(result, (leftMax[i - 1] - nums[i]) * static_cast<long long>(rightMax[i + 1]));
        }
        return result;
    }
};